﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SCMS.Reports;
using SCMS.UI.Models;
using SCMS.Model;

namespace SCMS.UI.Areas.Reports.Controllers
{
    public class SPController : Controller
    {
        //
        // GET: /Reports/SP/

        public ActionResult pdf(Guid SPid)
        {
            Dictionary<String, String> data = new Dictionary<string, string>();
            ViewSalaryPayment Model = SalaryPaymentExtension.PrepareSP(SPid);
            using (var db = new SCMSEntities())
            {
                data.Add("{VOUCHER_NO.}", "");
                data.Add("{REQ_NO.}", Model.EntitySalaryP.RefNumber);
                data.Add("{PAYROLL-MONTH}", Model.EntitySalaryP.PayrollMonth);
                data.Add("{PAYROLL-YEAR}", Model.EntitySalaryP.PayrollYear.ToString());
                data.Add("{CURRENCY}", Model.EntitySalaryP.Currency.ShortName);
                data.Add("{DATE}", Model.EntitySalaryP.PayrollDate.Value.ToString("dd/MM/yyyy"));
                data.Add("{OFFICE}", Model.EntitySalaryP.CountrySubOffice.Name);
                data.Add("{TOTAL-PAROLL}", Model.EntitySalaryP.TotalPayroll.ToString("##,###.00"));
                data.Add("{SUBJECT}", Model.EntitySalaryP.Subject);

                int count = 1;
                String detailHtml = "";
                foreach (var item in Model.EntitySalaryP.SalaryBudgetlines)
                {
                    count++;
                    detailHtml += "<tr><td style=\"width: 41%; text-align: left;\" colspan=\"2\">";
                    detailHtml += item.PayrollItem.Name;
                    detailHtml += "</td><td style=\"width: 19%; text-align: center;\">";
                    detailHtml += item.ProjectBudget.BudgetCategory.ProjectDonor.ProjectNumber;
                    detailHtml += "</td><td style=\"width: 19%; text-align: center;\">";
                    detailHtml += item.ProjectBudget.LineNumber;
                    detailHtml += "</td><td style=\"width: 19%; text-align: right;\">";
                    detailHtml += item.Amount.ToString("##,##0.00");
                    detailHtml += "</td></tr>";
                }
                while (count < 17)
                {
                    count++;
                    detailHtml += "<tr><td style=\"width: 41%; text-align: left;\" colspan=\"2\">&nbsp;";
                    detailHtml += "</td><td style=\"width: 19%; text-align: center;\">";
                    detailHtml += "</td><td style=\"width: 19%; text-align: center;\">";
                    detailHtml += "</td><td style=\"width: 19%; text-align: right;\">";
                    detailHtml += "</td></tr>";
                }
                data.Add("{DETAILS}", detailHtml);

                data.Add("{MB_CURRENCY}", "");//db.Currencies.FirstOrDefault(p => p.Id == Model.MBCurrencyId).ShortName);
                data.Add("{REMARKS}", Model.EntitySalaryP.Remarks);
                data.Add("{TOTAL}", "");//Model.MBValue.ToString("##,##0.00"));

                String authourisation = "";
                authourisation = AuthName(Model, authourisation);
                authourisation = AuthTitle(Model, authourisation);
                authourisation = AuthDate(Model, authourisation);
                authourisation = AuthSignature(Model, authourisation);
                data.Add("{AUTHORISATION}", authourisation);
            }

            Byte[] output = WkHtml2Pdf.CreateReport(data, "Salary-Payment.htm");
            return File(output, "application/pdf", "SP_" + DateTime.Now.FormatDDMMMYYYYHHmm());
        }

        private static String AuthName(ViewSalaryPayment Model, String authourisation)
        {

            authourisation += "<tr><th>Name:</th><td>";
            authourisation += Model.PreparedBy.StaffName;
            authourisation += "</td><td>";
            if (Model.ApprovedBy != null)
                authourisation += Model.ApprovedBy.StaffName;
            authourisation += "</td><td>";
            if (Model.AuthorizedBy != null)
                authourisation += Model.AuthorizedBy.StaffName;
            authourisation += "</td><tr>";

            return authourisation;
        }

        private static String AuthTitle(ViewSalaryPayment Model, String authourisation)
        {
            authourisation += "<tr><th>Title:</th><td>";
            authourisation += Model.PreparedBy.Designation;
            authourisation += "</td><td>";
            if (Model.ApprovedBy != null)
                authourisation += Model.ApprovedBy.Designation;
            authourisation += "</td><td>";
            if (Model.AuthorizedBy != null)
                authourisation += Model.AuthorizedBy.Designation;
            authourisation += "</td><tr>";
            return authourisation;
        }

        private static String AuthDate(ViewSalaryPayment Model, String authourisation)
        {
            authourisation += "<tr><th>Date:</th><td>";
            authourisation += Model.EntitySalaryP.PreparedOn.Value.ToString("dd/MM/yyyy h:mm tt");
            authourisation += "</td><td>";
            if (Model.EntitySalaryP.ReviewedOn != null)
                authourisation += Model.EntitySalaryP.ReviewedOn.Value.ToString("dd/MM/yyyy h:mm tt");
            authourisation += "</td><td>";
            if (Model.EntitySalaryP.AuthorizedOn != null)
                authourisation += Model.EntitySalaryP.AuthorizedOn.Value.ToString("dd/MM/yyyy h:mm tt");
            authourisation += "</td><tr>";
            return authourisation;
        }

        private static String AuthSignature(ViewSalaryPayment Model, String authourisation)
        {
            authourisation += "<tr><th>Signature:</th><td>";
            if (Model.PreparedBy.SignatureImage.IsNotNull())
                authourisation += string.Format("<img src=\"{0}\" alt=\"\" style=\"max-width: 100px;\" />", "data:image/png;base64," + Convert.ToBase64String(Model.PreparedBy.SignatureImage));
            authourisation += "</td><td>";
            if (Model.ApprovedBy != null && Model.ApprovedBy.SignatureImage.IsNotNull())
                authourisation += string.Format("<img src=\"{0}\" alt=\"\" style=\"max-width: 100px;\" />", "data:image/png;base64," + Convert.ToBase64String(Model.ApprovedBy.SignatureImage));
            authourisation += "</td><td>";
            if (Model.AuthorizedBy != null && Model.AuthorizedBy.SignatureImage.IsNotNull())
                authourisation += string.Format("<img src=\"{0}\" alt=\"\" style=\"max-width: 100px;\" />", "data:image/png;base64," + Convert.ToBase64String(Model.AuthorizedBy.SignatureImage));
            authourisation += "</td><tr>";
            return authourisation;
        }

    }
}
